<?php

namespace App\Models\Manager;

use App\Models\Manager\Group\Pivot\GroupPermissionPivot;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Group extends Model
{
    use HasFactory;
    protected $primaryKey = 'id';
    protected $table = 'group';
    protected $fillable = [
        'id',
        'name',
        'description',
        'state'
    ];

    public $timestamps = false;


    /** 组中的所有用户
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function User(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
    {
        return $this->belongsToMany(User::class,'group_user_relation','groupId','userId');
    }

    /** 组所包含的所有权限
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function Permission(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
    {
        return $this->belongsToMany(Permission::class,'group_permission_relation','groupId','permissionId')->using(GroupPermissionPivot::class)->withPivot(['id']);
    }

    public function GroupManager(){
        return $this->hasMany(GroupManager::class,'groupId','id');
    }
}
